************************************************************************************************************************
*                        PREPARE DATA FOR AND RUN TRAD MEDICARE REGRESSIONS WITH HETERO BY PREDICTED RISK              *
************************************************************************************************************************

capture mkdir "${SIDOutputPath}/reg_trad_predictedrisk_nopatientFEs/"
capture mkdir "${SIDOutputPath}/reg_trad_predictedrisk_patientFEs/"
capture mkdir "${SIDOutputPath}/Coefficients/reg_trad_predictedrisk_nopatientFEs/"
capture mkdir "${SIDOutputPath}/Coefficients/reg_trad_predictedrisk_patientFEs/"
*--------------------------------------------------
log using "${SIDCodePath}/sidsedd_regressions_riskpredict.log", replace

*--------------------------------------------------
* Keep only the variables needed for regression
*--------------------------------------------------
use edhour_2 dqtr year hcup_os age medicare trad_medicare dshospid ///
	 inpatient los totchg ndx died npr orproc tchg_lsvi age female sinceIP_30 sincevi* *lsvi  nchronic medincstq ///
	 pointoforigin_x race_x hispanic_x *nxtvi nxtvi* dx* md* key ///
	 dqtr_2 year_dqtr post_qtr after_MN  obs out_notobs post_qtr_after_MN ///
	 dshospid2 dshospid2 pointoforigin2 race2 hispanic2 agebin hourstilMN* using "${SIDDataPath}/sidsedd_merged_20102016.dta", clear

*--------------------------------------------------
* Merge in predicted risk
*--------------------------------------------------
merge 1:1 key using "${SIDDataPath}/riskpredict/fl_med_1016_all_pred.dta"

*--------------------------------------------------
* Regressions
*--------------------------------------------------
// Dep vars: inpatient status within 3 hours of midnight
foreach predvar of varlist pred_B4 {
	foreach var of varlist		inpatient nxtvi_30d{ 
		forval t = 3/3 {
			preserve
				keep	if trad_medicare == 1
				keep 	if hourstilMN`t' == 1

				reghdfe `var' post_qtr_after_MN##ib1.dec_`predvar', ///
						absorb(edhour_2 year_dqtr dshospid i.edhour_2##i.dshospid2 i.year_dqtr##i.dshospid2 agebin##female race2 hispanic2 pointoforigin2 sincevi_30 nchronic medincstq dec_`predvar') /// 
						vce(cluster edhour_2 year_dqtr)
				eststo 	`predvar'_`var'_`t'h_diRD, ///
						title(proper("`var'"))

				estadd   local hospitaltimeFE "X"
				estadd   local dxccsFE 	 	  ""
				estadd   local patientFE 	  "X"
				estadd 	 local MNsample 	  "`t'"
				estadd 	 local pop 			  "IP & OP"
				estadd   local medsample 	  "All"

				esttab ///
					using "${SIDOutputPath}/reg_trad_predictedrisk_patientFEs/reg_med_`var'_hetero_dec`predvar'_diRD.tex", replace /// 
					star(* .10 ** .05 *** .01) ///
					scalar("hospitaltimeFE Hospital-time FE" "dxccsFE Prim. Diag. FE" "patientFE Patient FE" "MNsample Midnight hours range" "pop Sample" "medsample Sample" "N Observations") /// 
					coeflabels(post_qtr_after_MN "$ Post_t \times AfterMN_h $") /// 
					b(%10.3f) noobs	sfmt(0)	nodepvars numbers se noconstant ///
					addnotes("Hospital-time FE: hospital, hospital-quarter, and hospital-hour")

				parmest, ///
						saving("${SIDOutputPath}/reg_trad_predictedrisk_patientFEs/Coefficients/reg_med_`var'_hetero_dec`predvar'_diRD.dta", replace)  
				eststo clear 
			restore

		}
	} 
}
				

log close
					
